package com.ysh.springbookdemo.service;

import com.ysh.springbookdemo.dao.BookDao;
import com.ysh.springbookdemo.enums.BookStatusEnum;
import com.ysh.springbookdemo.mapper.BookMapper;
import com.ysh.springbookdemo.model.BookInfo;
import com.ysh.springbookdemo.model.PageRequest;
import com.ysh.springbookdemo.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;


    public void addBook(BookInfo bookInfo){
        bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1.获取总图书数
        //2.获取当前页面数据
        Integer count = bookMapper.count();

        //对结果进行二次处理
        List<BookInfo> bookInfos = bookMapper.selectBookByPage(pageRequest);
        for(BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        /*ResponseResult<BookInfo> responseResult = new ResponseResult<>(count,bookInfos);
        responseResult.setTotal(count);
        responseResult.setRecords(bookInfos);*/
        return new ResponseResult<>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBookInfo(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }
}
